﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Arizu.DAL;
using Arizu.Entity;

namespace Arizu.Repository
{
    public class AirizuPrice : BaseProvider
    {
        #region
        private static volatile AirizuPrice instance;
        private static object lockHelper = new object();

        private AirizuPrice()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AirizuPrice CreateInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new AirizuPrice();
                    }
                }
            }
            return instance;
        }
        #endregion

        /// <summary>
        /// 插入评价信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public void Insert(PriceEntity price)
        {
            string sql = "INSERT INTO Price (DayPrice,WeekPrice,MonthPrice, GuestPrice, Fee, CheckIn, CheckOut, RoomID, Type, AdvancedPrice, AdditionalWeeklyPrice, AdditionalMonthlyPrice, GuestNo) VALUES (@DayPrice, @WeekPrice, @MonthPrice, @GuestPrice, @Fee, @CheckIn, @CheckOut, @RoomID, @Type, @AdvancedPrice, @AdditionalWeeklyPrice, @AdditionalMonthlyPrice, @GuestNo)";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@DayPrice", price.DayPrice);
            SqlParameter parm_2 = new SqlParameter("@WeekPrice", price.WeekPrice);
            SqlParameter parm_3 = new SqlParameter("@MonthPrice", price.MonthPrice);
            SqlParameter parm_4 = new SqlParameter("@GuestPrice", price.GuestPrice);
            SqlParameter parm_5 = new SqlParameter("@Fee", price.Fee);
            SqlParameter parm_6 = new SqlParameter("@CheckIn", price.CheckIn);
            SqlParameter parm_7 = new SqlParameter("@CheckOut", price.CheckOut);
            SqlParameter parm_8 = new SqlParameter("@RoomID", price.RoomID);
            SqlParameter parm_9 = new SqlParameter("@Type", price.Type);
            SqlParameter parm_10 = new SqlParameter("@AdvancedPrice", price.AdvancedPrice);
            SqlParameter parm_11 = new SqlParameter("@AdditionalWeeklyPrice", price.AdditionalWeeklyPrice);
            SqlParameter parm_12 = new SqlParameter("@GuestNo", price.GuestNo);
            SqlParameter parm_13 = new SqlParameter("@AdditionalMonthlyPrice", price.AdditionalMonthlyPrice);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3, parm_4, parm_5, parm_6, parm_7, parm_8, parm_9, parm_10, parm_11, parm_12, parm_13 };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

                //price.ID = long.Parse(ds.Tables[0].Rows[0][0].ToString());

            }
            catch (Exception)
            {

                throw;
            }
            //return price.ID;
        }

        /// <summary>
        /// 获取价格
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetPrice(long id)
        {
            DataTable dt = new DataTable();
            string sql = "select * from Price where RoomID=" + id;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public void UpdateWeekPricinglong(long id, int weeklyPricing, int additionalWeeklyPricing)
        {
            string sql = "Update Price Set WeekPrice=@WeekPrice, AdditionalWeeklyPrice=@AdditionalWeeklyPrice where RoomID=@RoomID";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@RoomID", id);
            SqlParameter parm_2 = new SqlParameter("@WeekPrice", weeklyPricing);
            SqlParameter parm_3 = new SqlParameter("@AdditionalWeeklyPrice", additionalWeeklyPricing);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3 };
            #endregion
            try
            {
                SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void UpdateAdditionalFee(long id, int fee)
        {
            string sql = "Update Price Set Fee=@Fee where RoomID=@RoomID";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@RoomID", id);
            SqlParameter parm_2 = new SqlParameter("@Fee", fee);
            SqlParameter[] parameters = { parm_1, parm_2 };
            #endregion
            try
            {
                SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void UpdateMonthPricing(long id, int weeklyPricing, int additionalWeeklyPricing)
        {
            string sql = "Update Price Set MonthPrice=@MonthPrice, AdditionalMonthlyPrice=@AdditionalMonthlyPrice where RoomID=@RoomID";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@RoomID", id);
            SqlParameter parm_2 = new SqlParameter("@MonthPrice", weeklyPricing);
            SqlParameter parm_3 = new SqlParameter("@AdditionalMonthlyPrice", additionalWeeklyPricing);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3 };
            #endregion
            try
            {
                SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void UpdateGuestNoFee(long id, int weeklyPricing, int additionalWeeklyPricing)
        {
            string sql = "Update Price Set GuestNo=@GuestNo, GuestPrice=@GuestPrice where RoomID=@RoomID";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@RoomID", id);
            SqlParameter parm_2 = new SqlParameter("@GuestNo", weeklyPricing);
            SqlParameter parm_3 = new SqlParameter("@GuestPrice", additionalWeeklyPricing);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3 };
            #endregion
            try
            {
                SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void UpdateDayPrice(long id, int price)
        {
            string sql = "Update Price Set DayPrice=@DayPrice where RoomID=@RoomID";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@RoomID", id);
            SqlParameter parm_2 = new SqlParameter("@DayPrice", price);
            SqlParameter[] parameters = { parm_1, parm_2 };
            #endregion
            try
            {
                SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void UpdateAdvancedPricing(long id, int price, long checkin, long checkout)
        {
            string sql = "Update Price Set CheckIn=@CheckIn, CheckOut=@CheckOut, AdvancedPrice=@AdvancedPrice where RoomID=@RoomID";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@RoomID", id);
            SqlParameter parm_2 = new SqlParameter("@CheckIn", checkin);
            SqlParameter parm_3 = new SqlParameter("@CheckOut", checkout    );
            SqlParameter parm_4 = new SqlParameter("@AdvancedPrice", price);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3, parm_4 };
            #endregion
            try
            {
                SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
